<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layuiAdmin std - 通用后台管理模板系统（iframe标准版）</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">

        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <button class="layui-btn layuiadmin-btn-list" data-type="add">添加</button>
            </div>
            <!--表格入口-->
            <table id="link" lay-filter="LAY-app-content-list"></table>

            <script type="text/html" id="tools">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i
                        class="layui-icon layui-icon-edit"></i>编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i
                        class="layui-icon layui-icon-delete"></i>删除</a>
            </script>
        </div>
    </div>
</div>

<script src="${pageContext.request.contextPath}/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '${pageContext.request.contextPath}/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'contlist', 'table'], function () {
        var table = layui.table
            , form = layui.form;

        //初始化表格
        table.render({
            elem: '#link'
            , height: 400
            , url: '${pageContext.request.contextPath}/admin/link' //数据接口
            , defaultToolbar: ['filter', 'print', 'exports']
            , title: '博客友链列表'
            , id: 'link' //这个就是表格重载的时候需要使用的字段
            , cols: [[ //表头
                {field: 'id', title: 'ID', width: 80, sort: true, fixed: 'left'}
                , {field: 'name', title: '友链名称', width: 260}
                , {field: 'url', title: '友链链接', width: 730}
                , {fixed: 'right', title: '操作', toolbar: '#tools', width: 180}
            ]]
        });
        //监听行工具事件 tool()里面填写的是table的filter
        table.on('tool(LAY-app-content-list)', function (obj) {
            let data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('真的删除这个友链信息吗', function (index) {
                    //数据持久化到数据库
                    $.post({
                        url: "${pageContext.request.contextPath}/admin/link?method=del",
                        data: {
                            id: data.id
                        },
                        success: function (data) {
                            if (data === "true") {
                                obj.del();
                                layer.close(index);
                            } else {
                                layer.alert("删除失败")
                            }
                        }
                    })
                });
                //数据修改
            } else if (obj.event === 'edit') {
                layer.open({
                    type: 2
                    , content: '../edit/editLink.jsp'
                    , area: ['450px', '250px']
                    , maxmin: true
                    , title: '修改友链'
                    //函数变量是固定的，不可以改变
                    , success: function (layero, index) {
                        var body = layer.getChildFrame('body', index);
                        //寻找数据填充地方
                        console.log(data.id)
                        body.find('input[name=id]').val(data.id)
                        body.find('input[name=name]').val(data.name)
                        body.find('input[name=url]').val(data.url)
                    }
                })
            }
        });


        var $ = layui.$, active = {
            add: function () {
                layer.open({
                    type: 2
                    , title: '添加友链信息'
                    , content: '../add/addLink.jsp'
                    , area: ['450px', '250px']
                    , maxmin: true
                    , yes: function (index, layero) {
                        var othis = layero.find('iframe').contents().find("#layuiadmin-app-form-tags")
                            , tags = othis.find('input[name="tags"]').val();
                        table.reload('link');
                        layer.close(index);
                    }
                });
            }
        }
        $('.layui-btn.layuiadmin-btn-list').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
</body>
</html>
